home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Practical Algorithms for Image Analysis
/
Practical Algorithms for Image Analysis.iso
/
CH_2.4
/
imgrotate
/
imgrotate.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-09-11
|
4KB
|
149 lines
/*
* imgrotate.c
*
* Practical Algorithms for Image Analysis
*
* Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
*/
/* IMGROTATE: program rotates image by given angle
* usage: imgrotate inimg outimg angle [-o X_ORIGIN Y_ORIGIN]
* [-q] [-L]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <tiffimage.h> /* tiff info on images */
#include <images.h> /* for sun images */
extern void print_sos_lic ();
int imrotate (Image *, Image *, double, long, long, short);
int usage (short);
int input (int, char **, double *, long *, long *, short *);
main (argc, argv)
int argc;
char *argv[];
{
Image *imgI, *imgO; /* I/O image structures */
unsigned char **imgIn, /* image array */
**imgOut; /* output image */
long widthI, heightI, /* input image size */
x0, y0; /* top left of output img in input img */
int gray; /* flag = 1 if image is gray; 0 otherwise */
double angle; /* angle to be rotated */
short quickFlag; /* bilinear interp if 0; nearest pix if 1 */
if ((input (argc, argv, &angle, &x0, &y0, &quickFlag)) < 0)
return (-1);
/* open input and output images */
imgI = ImageIn (argv[1]);
if (imgI->bps == 8 && imgI->spp == 3) {
printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
exit (1);
}
imgIn = imgI->img;
heightI = ImageGetHeight (imgI);
widthI = ImageGetWidth (imgI);
gray = (ImageIsGray (imgI)) ? 1 : 0;
printf ("image size is %dx%d\n", widthI, heightI);
imgO = ImageAlloc (heightI, widthI, 8); /* ImageGetDepth (imgI)); */
imgOut = ImageGetPtr (imgO);
/* set origin of rotation to be middle of image if default chosen */
if (x0 == -1) {
x0 = widthI / 2;
y0 = heightI / 2;
}
/* rotate image */
imrotate (imgI, imgO, angle, x0, y0, quickFlag);
/* write output rotated image */
ImageOut (argv[2], imgO);
return (0);
}
/* USAGE: function gives instructions on usage of program
* usage: usage (flag)
* When flag is 1, the long message is given, 0 gives short.
*/
int
usage (flag)
short flag; /* flag =1 for long message; =0 for short message */
{
/* print short usage message or long */
printf ("USAGE: imgrotate inimg outimg angle [-o X_ORIGIN Y_ORIGIN] [-q] [-L]\n");
if (flag == 0)
return (-1);
printf ("\nimgrotate rotates image by specified angle.\n\n");
printf ("ARGUMENTS:\n");
printf (" inimg: input image filename (TIF)\n");
printf (" outimg: output image filename (TIF)\n");
printf (" angle: value (in degrees) of desired rotation with respect\n");
printf (" to the horizontal; counter-clockwise is positive.\n\n");
printf ("OPTIONS:\n");
printf (" -o X_ORIGIN Y_ORIGIN: are coordinates about which to rotate.\n");
printf (" (0,0) is at top-left corner.\n");
printf (" Default is middle of image.\n");
printf (" -q: if set, performs quicker rotation by using\n");
printf (" nearest-pixel instead of default bilinear interpolation.\n");
printf (" -L: print Software License for this module\n");
return (-1);
}
/* INPUT: function reads input parameters
* usage: input (argc, argv, angle, x0, y0, &quickFlag)
*/
#define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
int
input (argc, argv, angle, x0, y0, quickFlag)
int argc;
char *argv[];
double *angle; /* angle of rotation */
long *x0, *y0; /* origin of rotation */
short *quickFlag; /* bilinear interp if 0; nearest pix if 1 */
{
long n;
double atof ();
if (argc < 4)
USAGE_EXIT (1);
*angle = atof (argv[3]);
*x0 = *y0 = -1;
*quickFlag = 0;
for (n = 4; n < argc; n++) {
if (strcmp (argv[n], "-o") == 0) {
if (++n == argc || argv[n][0] == '-')
USAGE_EXIT (0);
*x0 = atol (argv[n]);
*y0 = atol (argv[++n]);
}
else if (strcmp (argv[n], "-q") == 0)
*quickFlag = 1;
else if (strcmp (argv[n], "-L") == 0) {
print_sos_lic ();
exit (0);
}
else
USAGE_EXIT (0);
}
return (0);
}